package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.MobileOrderService;
import cn.itcast.service.OrderListService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

@Slf4j
@RestController
@RequestMapping(path = "/order")
@CrossOrigin
public class OrderController {

    @Reference
    private MobileOrderService mobileOrderService;


    @Reference
    private OrderListService orderListService;

    /**
     * 提交预约信息
     * @param param
     * @return
     */
    @PostMapping("/submit")
    public Result submitOrder(@RequestBody Map<String,String> param){
        try {
            System.out.println(param);

            //进行验证码比对，如果不成功返回信息到页面
            param.put("orderType",Order.ORDERTYPE_WEIXIN);
            String phone = param.get("phoneNumber");
            String code = param.get("validateCode");

            // 将数据上传到数据库中
            this.orderListService.saveOrder(param);

            Order order = this.mobileOrderService.findOrderByUserName(param.get("name"));

            return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ORDER_FAIL);
    }

    /**
     * 根据预约id，查询套餐信息和地址信息
     * @param id 预约id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") String id){
        System.out.println("预约id为： " + id );

        try {
            Order order = this.mobileOrderService.findOrderById(id);

            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
    }
}
